#ifndef __CASE_TPL_MEMORY_H__
#define __CASE_TPL_MEMORY_H__

#include <iostream>
#include <gtest/gtest.h>
#include "basic/memory_pool.h"

class Test_dk_memory : public testing::Test {
protected:
  struct S {
    int i = 10;
    float f = 11.1f;
    void Add100() {
      i += 100;
      f += 100.f;
    }
  };
  basic::DKMemoryAllocator* a = nullptr;
  virtual void SetUp() {
    a = new basic::DKMemoryAllocator();
    a->SetMemoryBlock(new basic::DKMemoryBlock(1 << 10)); // 1KiB
  };
  virtual void TearDown() {
    delete a;
    a = nullptr;
  };
};

TEST_F(Test_dk_memory, value) {
  Test_dk_memory::S* pS = a->Allocate<Test_dk_memory::S>(1);
  ASSERT_EQ(pS->i, 10);
  ASSERT_EQ(pS->f, 11.1f);
}

TEST_F(Test_dk_memory, overflow) {
  Test_dk_memory::S* pS = a->Allocate<Test_dk_memory::S>(10000);
  ASSERT_EQ(pS, nullptr);
}

TEST_F(Test_dk_memory, freeAll) {
  Test_dk_memory::S* pS = a->Allocate<Test_dk_memory::S>(100);
  ASSERT_NE(pS, nullptr);
  a->FreeAll();
  Test_dk_memory::S* pS2 = a->Allocate<Test_dk_memory::S>(10);
  ASSERT_NE(&pS, &pS2);
}

TEST_F(Test_dk_memory, changeValue) {
  Test_dk_memory::S* pS = a->Allocate<Test_dk_memory::S>(1);
  pS->i = 1000;
  ASSERT_EQ(1000, pS->i);
  pS->Add100();
  ASSERT_EQ(1100, pS->i);
  ASSERT_EQ(111.1f, pS->f);
}

#endif
